<?php
namespace Erp\C\Orders\Tn;
use Erp\C\Orders\Tn\Shipping\Eub;
use Erp\C\Orders\Tn\Shipping\Yw;
use Erp\C\Orders\Tn\Shipping\CF;
use Erp\C\Orders\Tn\Shipping;
use Erp\C;
use Fn;
/**
 *ERP-TN管理
 *注:api获取的运输方式，必须把api类型添加至属性字段$_aipType内
 */
class ShippingTn{
    private $api='',$type='',$limit=100;
    private $_shippingClsObj = array();
    private $_aipType=array(
        'wb','yt','yd','jumia','ws','cndhl','edis','est','hl','ykd','yym','ty','lwe','ubi','tms','gd','kts','ft','ib','gati','dq','ost','dtdc',
        'ispoversea','sp','cp','isp','dsf','yw','ceub','eub','wish','apac','eyb','sfc','cky','lazada','aliexpress','sy','dsfoversea'
    );
    private function _getShippingObj($cls)
    {
        if(!isset( $this->_shippingClsObj[$cls] )){
            $cls = $this->getShippingCls($cls);
            $this->_shippingClsObj[$cls] = new $cls();
        }
        return $this->_shippingClsObj[$cls];
    }

    function getShippingCls($type)
    {
        return 'Erp\C\Orders\Tn\Shipping\\'.$type;
    }
    /**
     *指定orders_id进行获取tn
     *@param array||string $ordersId 一维
     */
    public function getTnByOrders($ordersId){
        if(!is_array($ordersId)){
            $ordersId=array($ordersId);
        }
        if(!$ordersId) return false;
        $res = A('Orders')->getOrders($ordersId,false);
        $this->_aipType;
         foreach ($res as $k=>$v){
            $shippingCode=$v['orders_shipping_method'];
            $shippingType=$this->getTypeByCode($shippingCode);
            $shippingType=ucwords(strtolower($shippingType));
            $this->api=$this->_getShippingObj($shippingType);
            $this->api->getTn(array($v['orders_id']));
         }
    }
    /**
     *指定orders_id获取Lable标签面单
     *@param
     */
    public function getLableByOrders($ordersId){
        $this->_aipType;
        if(!is_array($ordersId)){
            $ordersId=array($ordersId);
        }
        if(!$ordersId) return false;
        $res = A('Orders')->getOrders($ordersId);
        new C\Orders\OrdersLabel(true);
		foreach ($res as $k=>$v){
            if(!$v['orders_tn'])continue;
            $shippingCode=$v['orders_shipping_method'];
            $shippingType=$this->getTypeByCode($shippingCode);	//根据运输方式获取API类型
            $this->getPdfDir($v['orders_id']);//检测pdf是否存在
            $shippingType=ucwords(strtolower($shippingType));
            $this->api=$this->_getShippingObj($shippingType);
            $this->api->getLable(array($v['orders_id'])); //生成面单步骤
		}
    }
    /**
     *指定EUB orders_id手动提单
     */
    public function putOrdersById($ordersId){
        $model=A("Orders");
        $res = $model->getOrders($ordersId);
        foreach($res as $k=>$v){
            $shipping=$v['orders_shipping_method'];
            $shippingType=$this->getTypeByCode($shipping);
            $shippingType=ucwords(strtolower($shippingType));
            if($v['orders_id']=='1101122091'){
                $shippingType='Apac';
            }
            if($shippingType!='Eub' && $shippingType!='Apac'){
                continue;
            }
            $cls=$this->getShippingCls($shippingType);
            $this->api=new $cls();
            $this->api->putOrders($v['orders_id']);
        }
    }

    //提单入口 只支持Eub  $orders_id=array(1,2,3...)
    public function submitOrder($orders_id){
        $api=$this->api;
        $api->putOrders($orders_id);
    }

    //各个运输方式CODE条件
    private function shippingCode($type){
        $code=$this->shippingTypeCode();
        $t=strtoupper($type);
        $shipping=$code[$t];
        if(!isset($shipping)||empty($shipping)){
            return false;
        }
        $where="'".implode("','", $shipping)."'";
        $where="orders_shipping_method in(".$where.")";
        return $where;
    }

    //需要API去获取TN的各个类型  eg.  Yw  Eub  Erp
    // private function getShippingType(){
    // 	$type=M("OrdersShippingMethod")->field("shipping_method_type")->where("shipping_method_type!='' and shipping_method_status=9")->group("shipping_method_type")->select();
    // 	$tmp=array();
    // 	foreach ($type as $k=>$v){
    // 		$tmp[]=$v['shipping_method_type'];
    // 	}
    // 	return $tmp;
    // }

    //根据运输方式去获取对应的API类型
    public function getTypeByCode($shippingCode){
        return A("OrdersShippingMethod")->getShippingTypeByCode($shippingCode);
    }

    //获TN的运输方式的集合
    public function shippingTypeCode(){
        // $res=D("OrdersShippingMethod")->where("shipping_method_type!='' and shipping_method_status=9")->select();
        $res = A('OrdersShippingMethod')->getShippingMethod();
        $tmp=array();
        foreach ($res as $k=>$v){
            $tmp[$v['shipping_method_type']][]=$v['shipping_method_code'];
        }
        return $tmp;
    }

    /**
     *实时获取本地运输方式TN
     *@param str $shippingWays 运输方式 , number  $limit  数量 默认一条
     *@return array
     */

    public function getLocalTn($shippingWays,$limit=1,$id)
    {
        $tn=$this->_getShippingObj('Erp')->getLocalTn($shippingWays,$limit,$id);
        return $tn;
    }

    /**
     *标记本地TN未使用
     *@param  string ||  array     $tn
     */
    public  function cancelLocalTn($tn){
        if(!is_array($tn)){
            $tn=array($tn);
        }
        $api=new Erp();
        $api->cancelTnOccupy($tn);
    }

    /**
     *根据ID获取pdf面单路径
     *@param string  $ordersId,
     *@param [bool $dir 真获取绝对路径，默认获取相对]
     */
    public function getPdfDir($ordersId,$dir=false){
        if(!$dir){
            $root_dir="./Public/Data/Files/";
        }else{
            $root_dir=Fn\App::appAbsoluteDir();
            $root_dir=$root_dir."Public/Data/Files/";
        }
        // $res=$model->where("orders_id=$ordersId")->find();
        $res = A('Orders')->getOrders($ordersId);
        $importTime=$res['orders_import_time'];
        $file=date('Ymd',$importTime)."/".$ordersId.".pdf";
        return $root_dir.$file;
    }

    //返回运输方式
    // public function returnShippingCode(){
    // 	$res=M("OrdersShippingMethod")->select();
    // 	$tmp=array();
    // 	foreach ($res as $k=>$v){
    // 		$tmp[$v['shipping_method_code']]=$v;
    // 	}
    // 	return $tmp;
    // }

}
?>